#include "demo.h"
#include <malloc.h>

static int Demo_Add(Demo* pThis, int v)
{
	struct ClassDemo* demo = (struct ClassDemo*)pThis;
	return demo->mi + demo->mj + v;
}

static int Derived_Add(Demo* pThis, int v)
{
	struct ClassDerived* derived = (struct ClassDerived*)pThis;
	return derived->demo.mi + derived->demo.mj + derived->mk + v;
}

static struct VirtTlb g_demoTlb = {
	Demo_Add
};

static struct VirtTlb g_derivedTlb = {
	Derived_Add
};

Demo* Create_DemoObj(int i, int j)
{
	struct ClassDemo* ret = (struct ClassDemo*)malloc(sizeof(struct ClassDemo));
	if (NULL != ret)
	{
		ret->pvtl = &g_demoTlb;
		ret->mi = i;
		ret->mj = j;
	}

	return ret;
}

void Destory_DemoObj(void* pThis)
{
	free(pThis);
}

int getI(Demo* pThis)
{
	struct ClassDemo* demo = (struct ClassDemo*)pThis;
	return demo->mi;
}

int getJ(Demo* pThis)
{
	struct ClassDemo* demo = (struct ClassDemo*)pThis;
	return demo->mj;
}

int add(Demo* pThis, int v)
{
	struct ClassDemo* demo = (struct ClassDemo*)pThis;
	return demo->pvtl->pAdd(demo, v);
}


Derived* Create_DerivedObj(int i, int j, int k)
{
	struct ClassDerived* ret = (struct ClassDerived*)malloc(sizeof(struct ClassDerived));
	if (NULL != ret)
	{
		ret->demo.pvtl = &g_derivedTlb;
		ret->demo.mi = i;
		ret->demo.mj = j;
		ret->mk = k;
	}

	return ret;
}

int getK(Derived* pThis)
{
	struct ClassDerived* derived = (struct ClassDerived*)pThis;
	return derived->mk;
}